{% extends "webhooks/base.html" %}

{% block content %}
	<section id="webhooks-form" class="box-section">
	<h3>{% if webhook %}Edit webhook{% else %}Add webhook{% endif %}</h3>

	<div class="inner">
		{% if not webhook %}
		<p>
			Whenever an uploaded game finishes processing, a configured webhook
			will send an HTTP POST to the configured URL.<br/>
			The data will contain a JSON representation of the game's metadata.
		</p>
		<p>
			This is useful if you want to build applications which will react to a
			completed game.
			The time from upload to trigger should be, on average, below 30 seconds.
		</p>
		<hr/>
		{% endif %}

		<form action="" method="POST" id="webhook-create-form">
			{% csrf_token %}

			{% if webhook %}
			<div class="form-group">
				<label for="uuid">UUID</label>
				<input
					type="text" id="uuid" class="form-control" value="{{ webhook.uuid }}" readonly
				/>
			</div>
			{% endif %}

			<div class="form-group">
				<label for="id_url">Payload URL</label>
				<input
					type="url" name="url" id="id_url" size="30" maxlength="200"
					required="required" placeholder="https://example.com/..."
					class="form-control" value="{{ form.instance.url }}"
				/>
			</div>

			{% if form.url.errors %}
				<div class="alert alert-danger">{{ form.url.errors }}</div>
			{% endif %}
			<p>
				This URL will receive the JSON POST request.
				It should always return either HTTP 200 or HTTP 201.
			</p>

			<p>
				{{ form.is_active }}
				<label for="id_is_active">Active</label>
			</p>

			{% if webhook %}
				<button type="submit" class="btn btn-primary">Update webhook</button>
				{% if user.is_staff %}
					<a href="{% url 'admin:webhooks_webhook_change' webhook.pk %}" class="btn btn-info pull-right">View in admin</a>
				{% endif %}
			{% else %}
				<button type="submit" class="btn btn-success">Add webhook</button>
			{% endif %}
		</form>

		{% if webhook %}
			<form
				action="{% url 'account_delete_webhook' webhook.pk %}"
				method="POST"
				id="webhook-delete-form"
				onsubmit="return confirm('Are you sure? There is no undo.')"
			>
				{% csrf_token %}
				<input type="submit" class="btn btn-danger" value="Delete webhook"/>
			</form>
		{% endif %}
	</div>

	</section>

	{% if webhook %}
		<section id="webhooks-deliveries" class="box-section">
		<h3>Deliveries</h3>
		<div class="inner">
		{% with webhook.triggers.all as triggers %}
			{% if triggers %}
				<table class="table table-bordered">
					<thead>
						<th>URL</th>
						<th>Date</th>
						<th>Success</th>
					</thead>
					{% for trigger in triggers %}
						<tr>
							<td><a href="{{ trigger.url }}">{{ trigger.url }}</a></td>
							<td><time datetime="{{ trigger.created|date:'c' }}">{{ trigger.created }}</time></td>
							<td>
								{% if trigger.success %}
									<strong class="webhook-active">✓</strong>
									({{ trigger.completed_time }}ms)
								{% else %}
									<strong class="webhook-inactive">✗</strong>
									{% if trigger.response_status %}(HTTP {{ trigger.response_status }}){% endif %}
								{% endif %}
							</td>
						</tr>
					{% endfor %}
				</table>
			{% else %}
				<p>
					This webhook has not been triggered yet.
					{% if not webhook.is_active %}
						<em>Try enabling it!</em>
					{% endif %}
				</p>
			{% endif %}
		{% endwith %}
		</div>
		</section>
	{% endif %}
{% endblock %}
